package com.laboratory.report.lab05.lab0502;

import lombok.Getter;
import lombok.SneakyThrows;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 测试
 */
public class Lab0502Main {

    @SneakyThrows
    public static void main(String[] args) {
        ExecutorService es = Executors.newFixedThreadPool(3);

        Timer timer = new Timer(3);
        Queue queue1 = new Queue("队伍1", timer);
        Queue queue2 = new Queue("队伍2", timer);

        Future<?> future = es.submit(timer);
        es.submit(queue1);
        es.submit(queue2);

        future.get();
    }

    public static class Queue extends Thread {

        private String queueName;
        private Timer Timer;

        public Queue(String queueName, Timer Timer) {
            this.queueName = queueName;
            this.Timer = Timer;
        }

        @SneakyThrows
        @Override
        public void run() {
            while(Timer.getRest()>0) {
                System.out.println("队伍休息中...");
                Thread.sleep(1000);
            }
            System.out.println("队伍比赛中...");
        }
    }

    @Getter
    public static class Timer extends Thread {

        private int rest;

        public Timer(int rest) {
            this.rest = rest;
        }

        @SneakyThrows
        @Override
        public void run() {
            while(rest>0) {
                System.out.println("中场休息时间剩余: " + rest + "分钟");
                --rest;
                Thread.sleep(1000);
            }
            System.out.println("休息时间结束，比赛开始！");
        }
    }
}
